package com.red.wood.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DateUtil {
	
	private static final Log log = LogFactory.getLog(DateUtil.class);
	
	private static String ymdhms = "yyyy-MM-dd HH:mm:ss";
	private static String ymd = "yyyy-MM-dd";

	public static SimpleDateFormat ymdSDF = new SimpleDateFormat(ymd);
	private static String year = "yyyy";
	private static String month = "MM";
	private static String day = "dd";
	private static String hour = "HH";
	public static SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat(ymdhms);
	public static SimpleDateFormat yearSDF = new SimpleDateFormat(year);
	public static SimpleDateFormat monthSDF = new SimpleDateFormat(month);
	public static SimpleDateFormat daySDF = new SimpleDateFormat(day);
	public static SimpleDateFormat hourSDF = new SimpleDateFormat(hour);

	public static SimpleDateFormat yyyyMMddHHmm = new SimpleDateFormat("yyyy-MM-dd HH:mm");

	public static SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");

	public static SimpleDateFormat yyyyMMddHH_NOT_ = new SimpleDateFormat("yyyyMMdd");

	public static long DATEMM = 86400L;

	
	/**
	 * 获得当前时间 格式：2014-12-02 10:38:53
	 * 
	 * @return String
	 */
	public static String getCurrentTime() {
		try{
			return yyyyMMddHHmmss.format(new Date());
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return "";
	}

	/**
	 * 可以获取昨天的日期 格式：2014-12-01
	 * 
	 * @return String
	 */
	public static String getYesterdayYYYYMMDD() {
		Date date = new Date(System.currentTimeMillis() - DATEMM * 1000L);
		String str = yyyyMMdd.format(date);
		try {
			date = yyyyMMddHHmmss.parse(str + " 00:00:00");
			return yyyyMMdd.format(date);
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return "";
	}

	/**
	 * 可以获取后退N天的日期 格式：传入2 得到2014-11-30
	 * 
	 * @param backDay
	 * @return String
	 */
	public String getStrDate(String backDay) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, Integer.parseInt("-" + backDay));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String back = sdf.format(calendar.getTime());
		return back;
	}

	/**
	 * 获取当前的年、月、日
	 * 
	 * @return String
	 */
	public static int getCurrentYear(Date date) {
		try{
			return Integer.parseInt(yearSDF.format(date));
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return 2017;
	}
	
	public static String getCurrentYear_s(Date date) {
		try{
			return yearSDF.format(date);
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return "2017";
	}
	public static int getHalfYear(Date date){
		int m = getCurrentMonth(date);
		int halfYear = 0;
		if(m >= 1 && m <= 6){
			halfYear = 0;
		}else{
			halfYear = 1;
		}
		return halfYear;
	}

	public static int getCurrentMonth(Date date) {
		try{
			return Integer.parseInt(monthSDF.format(date));
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return 1;
	}
	public static String getCurrentMonth_s(Date date) {
		try{
			return monthSDF.format(date);
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return "1";
	}
	
	public static int getQuarter(Date date){
		int m= getCurrentMonth(date);
		int quarter = 1;
		if (m == 1 || m == 2 || m == 3){
			quarter = 1;
		}else if (m == 4 || m == 5 || m == 6){
			quarter = 2;
		}else if (m == 7 || m == 8 || m == 9){
			quarter = 3;
		}else if (m == 10 || m == 11 || m == 12){
			quarter = 4;
		}
		return quarter;
	}

	public static int getCurrentDay(Date date) {
		try{
			return Integer.parseInt(daySDF.format(date));
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return 1;
	}
	
	public static int getCurrentHour(Date date) {
		try{
			return Integer.parseInt(hourSDF.format(date));
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return 1;
	}
	
	
	public static String getCurrentHour_STR(Date date) {
		try{
			return hourSDF.format(date);
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return "01";
	}
	
	public static int getCurrentWeek(Date date) {
		try{
			Calendar calendar = Calendar.getInstance();
			calendar.setFirstDayOfWeek(Calendar.MONDAY);
			calendar.setTime(date);
			return calendar.get(Calendar.WEEK_OF_YEAR);
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(), e);
			}
		}
		return 1;
	}
	
	public static String getCurrentWeek_s(Date date) {
		try{
			Calendar calendar = Calendar.getInstance();
			calendar.setFirstDayOfWeek(Calendar.MONDAY);
			calendar.setTime(date);
			return String.valueOf(calendar.get(Calendar.WEEK_OF_YEAR));
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(), e);
			}
		}
		return "1";
	}

	/**
	 * 获取年月日 也就是当前时间 格式：2014-12-02
	 * 
	 * @return String
	 */
	public static String getCurrentymd(Date date) {
		try{
			return ymdSDF.format(date);
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return "";
	}

	public static String getyyyyMMddHH(Date date) {
		try{
			return yyyyMMddHH_NOT_.format(date);
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return "";
	}
	
	/**
	 * 获取当前时间前一个小时
	 * @return
	 */
	public static int getPreviousHour(Date date){
		try{
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.set(Calendar.HOUR_OF_DAY,calendar.get(Calendar.HOUR_OF_DAY) - 1);
			return getCurrentHour(calendar.getTime());
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return 1;
	}
	
	/**
	 * 当前时间前一个小时
	 * @return
	 */
	public static Date getPreviousDayHour(Date date){
		try{
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.set(Calendar.HOUR_OF_DAY,calendar.get(Calendar.HOUR_OF_DAY) - 1);
			return calendar.getTime();
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return new Date();
	}
	
	/**
	 * 获取昨天时间
	 * @return
	 */
	public static Date getYesterday(){
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		return cal.getTime();
	}
	
	/**
	 * 获取今天0点开始的秒数
	 * 
	 * @return long
	 */
	public static long getTimeNumberToday() {
		Date date = new Date();
		String str = yyyyMMdd.format(date);
		try {
			date = yyyyMMdd.parse(str);
			return date.getTime() / 1000L;
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return 0L;
	}

	/**
	 * 获取今天的日期 格式：20141202
	 * 
	 * @return String
	 */
	public static String getTodateString() {
		String str = yyyyMMddHH_NOT_.format(new Date());
		return str;
	}

	/**
	 * 获取昨天的日期 格式：20141201
	 * 
	 * @return String
	 */
	public static String getYesterdayString() {
		Date date = new Date(System.currentTimeMillis() - DATEMM * 1000L);
		String str = yyyyMMddHH_NOT_.format(date);
		return str;
	}

	/**
	 * 获得昨天零点
	 * 
	 * @return Date
	 */
	public static Date getYesterDayZeroHour() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.HOUR, 0);
		return cal.getTime();
	}

	/**
	 * 把long型日期转String ；---OK
	 * 
	 * @param date
	 *            long型日期；
	 * @param format
	 *            日期格式；
	 * @return
	 */
	public static String longToString(long date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		// 前面的lSysTime是秒数，先乘1000得到毫秒数，再转为java.util.Date类型
		java.util.Date dt2 = new Date(date * 1000L);
		String sDateTime = sdf.format(dt2); // 得到精确到秒的表示：08/31/2006 21:08:00
		return sDateTime;
	}

	/**
	 * 获得今天零点
	 * 
	 * @return Date
	 */
	public static Date getTodayZeroHour() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.HOUR, 0);
		return cal.getTime();
	}

	/**
	 * 获得昨天23时59分59秒
	 * 
	 * @return
	 */
	public static Date getYesterDay24Hour() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.HOUR, 23);
		return cal.getTime();
	}

	/**
	 * String To Date ---OK
	 * 
	 * @param date 待转换的字符串型日期；
	 * @param format 转化的日期格式
	 * @return 返回该字符串的日期型数据；
	 */
	public static Date stringToDate(String date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(date);
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
			return null;
		}
	}

	/**
	 * 获得指定日期所在的自然周的第一天，即周日
	 * 
	 * @param date 日期
	 * @return 自然周的第一天
	 */
	public static Date getStartDayOfWeek(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, 1);
		date = c.getTime();
		return date;
	}

	/**
	 * 获得指定日期所在的自然周的最后一天，即周六
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfWeek(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, 7);
		date = c.getTime();
		return date;
	}

	/**
	 * 获得指定日期所在当月第一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getStartDayOfMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, 1);
		date = c.getTime();
		return date;
	}

	/**
	 * 获得指定日期所在当月最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DATE, 1);
		c.add(Calendar.MONTH, 1);
		c.add(Calendar.DATE, -1);
		date = c.getTime();
		return date;
	}

	/**
	 * 获得指定日期的下一个月的第一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getStartDayOfNextMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, 1);
		c.set(Calendar.DAY_OF_MONTH, 1);
		date = c.getTime();
		return date;
	}

	/**
	 * 获得指定日期的下一个月的最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfNextMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DATE, 1);
		c.add(Calendar.MONTH, 2);
		c.add(Calendar.DATE, -1);
		date = c.getTime();
		return date;
	}

	/**
	 * 
	 * 求某一个时间向前多少秒的时间(currentTimeToBefer)---OK
	 * 
	 * @param givedTime 给定的时间
	 * @param interval 间隔时间的毫秒数；计算方式 ：n(天)*24(小时)*60(分钟)*60(秒)(类型)
	 * @param format_Date_Sign 输出日期的格式；如yyyy-MM-dd、yyyyMMdd等；
	 */
	public static String givedTimeToBefer(String givedTime, long interval,
			String format_Date_Sign) {
		String tomorrow = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format_Date_Sign);
			Date gDate = sdf.parse(givedTime);
			long current = gDate.getTime(); // 将Calendar表示的时间转换成毫秒
			long beforeOrAfter = current - interval * 1000L; // 将Calendar表示的时间转换成毫秒
			Date date = new Date(beforeOrAfter); // 用timeTwo作参数构造date2
			tomorrow = new SimpleDateFormat(format_Date_Sign).format(date);
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return tomorrow;
	}

	/**
	 * 把String 日期转换成long型日期；---OK
	 * 
	 * @param date String 型日期；
	 * @param format 日期格式；
	 * @return
	 */
	public static long stringToLong(String date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date dt2 = null;
		long lTime = 0;
		try {
			dt2 = sdf.parse(date);
			// 继续转换得到秒数的long型
			lTime = dt2.getTime() / 1000;
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}

		return lTime;
	}

	/**
	 * 得到二个日期间的间隔日期；
	 * 
	 * @param endTime 结束时间
	 * @param beginTime 开始时间
	 * @param isEndTime 是否包含结束日期；
	 * @return
	 */
	public static Map<String, String> getTwoDay(String endTime,
			String beginTime, boolean isEndTime) {
		Map<String, String> result = new HashMap<String, String>();
		if ((endTime == null || endTime.equals("") || (beginTime == null || beginTime
				.equals(""))))
			return null;
		try {
			java.util.Date date = ymdSDF.parse(endTime);
			endTime = ymdSDF.format(date);
			java.util.Date mydate = ymdSDF.parse(beginTime);
			long day = (date.getTime() - mydate.getTime())
					/ (24 * 60 * 60 * 1000);
			result = getDate(endTime, Integer.parseInt(day + ""), isEndTime);
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
		return result;
	}

	/**
	 * 得到二个日期间的间隔日期；
	 * 
	 * @param endTime 结束时间
	 * @param beginTime  开始时间
	 * @param isEndTime  是否包含结束日期；
	 * @return
	 */
	public static Integer getTwoDayInterval(String endTime, String beginTime,
			boolean isEndTime) {
		if ((endTime == null || endTime.equals("") || (beginTime == null || beginTime
				.equals(""))))
			return 0;
		long day = 0l;
		try {
			java.util.Date date = ymdSDF.parse(endTime);
			endTime = ymdSDF.format(date);
			java.util.Date mydate = ymdSDF.parse(beginTime);
			day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
			return 0;
		}
		return Integer.parseInt(day + "");
	}

	/**
	 * 根据结束时间以及间隔差值，求符合要求的日期集合；
	 * 
	 * @param endTime
	 * @param interval
	 * @param isEndTime
	 * @return
	 */
	public static Map<String, String> getDate(String endTime, Integer interval,
			boolean isEndTime) {
		Map<String, String> result = new HashMap<String, String>();
		if (interval == 0 || isEndTime) {
			if (isEndTime)
				result.put(endTime, endTime);
		}
		if (interval > 0) {
			int begin = 0;
			for (int i = begin; i < interval; i++) {
				endTime = givedTimeToBefer(endTime, DATEMM, ymd);
				result.put(endTime, endTime);
			}
		}
		return result;
	}
	/**
	 * 获取执行时间
	 * -d 格式2017041217
	 */
	public static Date getExecDate(Date date,String dayStr) {
		// 通用属性
		if (StringUtils.isNotBlank(dayStr)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
			try {
				date = sdf.parse(dayStr);
			} catch (ParseException e) {
				if(log.isErrorEnabled()){
					log.error(e.getMessage(),e);
				}
			}
		}
		return date;
	}
	public static Date getExecDate1(Date date,String darStr) {
		// 通用属性
		if (StringUtils.isNotBlank(darStr)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			try {
				date = sdf.parse(darStr);
			} catch (ParseException e) {
				if(log.isErrorEnabled()){
					log.error(e.getMessage(),e);
				}
			}
		}
		return date;
	}
}
